//
//  EPMHomeViewModel.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/6.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMHomeViewModel.h"
#import "EPMHomeCategoryModel.h"
#import "EPMActivityModel.h"

@implementation EPMHomeViewModel

+ (instancetype)sheardManger{
    static EPMHomeViewModel* _instance;
    static dispatch_once_t token;
    
    dispatch_once(&token,^{
        _instance = [EPMHomeViewModel new];
    });
    
    return _instance;
}

//请求商品数据
- (NSDictionary *)requestHomeInfomation {
    NSString *jsonStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"focus.json" ofType:@"php"] encoding:0 error:nil];
    
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingAllowFragments error:nil];
    return jsonDict;
}

//商品数据转模型
- (NSArray*)getModels{
    NSDictionary *goodsDict = [_HomeManager requestHomeInfomation];
    NSArray *goodsArr = goodsDict[@"data"][@"act_info"][3][@"act_rows"];
    NSMutableArray* tempArrList = [NSMutableArray array];
    
    for (NSInteger i =0; i<5; i++) {
        NSArray *goods = goodsArr[i][@"category_detail"][@"goods"];
        
        NSMutableArray* tempList = [NSMutableArray array];
        for (NSDictionary* dict in goods) {
            
            EPMGoodsModel* model = [EPMGoodsModel new];
            [model setValuesForKeysWithDictionary:dict];
            [tempList addObject:model];
        }
        
        [tempArrList addObject:tempList.copy];
    }
    return tempArrList.copy;
}

- (NSArray*)goodList{
    if(_goodList == nil){
        _goodList = [self getModels];
    }
    return _goodList;
}


//请求分类数据
- (NSArray*)getBtns{
    NSDictionary *buttonDict = [_HomeManager requestHomeInfomation];
    NSArray *buttonArr = buttonDict[@"data"][@"act_info"][1][@"act_rows"];
    NSMutableArray* tempArr = [NSMutableArray array];
    
    for (int i=0; i<4;i++) {
       
        EPMHomeCategoryModel* model = [EPMHomeCategoryModel new];
        NSString* nameStr = buttonArr[i][@"activity"][@"name"];
        const char *c = [nameStr cStringUsingEncoding:NSISOLatin1StringEncoding];
        model.name = [[NSString alloc] initWithCString:c encoding:NSUTF8StringEncoding];
        model.img = buttonArr[i][@"activity"][@"img"];
        [tempArr addObject:model];
    }
    
    return tempArr.copy;
}

- (NSArray*)categoryList{
    if(_categoryList == nil){
        _categoryList = [self getBtns];
    }
    return _categoryList;
}

//获取活动数据

- (NSArray*)getActivitys{
    NSDictionary *activityDict = [_HomeManager requestHomeInfomation];
    NSArray *activityArr = activityDict[@"data"][@"act_info"][3][@"act_rows"];

    NSMutableArray* tempList = [NSMutableArray array];
      for (NSInteger i =0; i<5; i++) {

        EPMActivityModel* model = [EPMActivityModel new];
        model.img = activityArr[i][@"activity"][@"img"];
        NSString* nameStr = activityArr[i][@"category_detail"][@"name"];
        const char *c = [nameStr cStringUsingEncoding:NSISOLatin1StringEncoding];
        model.name = [[NSString alloc] initWithCString:c encoding:NSUTF8StringEncoding];
          [tempList addObject:model];
          model.goods = self.goodList[i];
    }
    return tempList.copy;
    
}

- (NSArray*)activityList{
    if (_activityList == nil){
        _activityList = [self getActivitys];
    }
    return _activityList;
}


@end
